﻿MPT.addAction("microblog_space", function(_elm) {

    //设置4个微博标签
    var nav_jQs = $('.e_nav', _elm);
    var microblog_jQ = $('.e_microblog', _elm);

    var everyone_jQ = $('.e_everyone', _elm);
    var friend_group_jQ = $('.e_friend_group', _elm);
    var follow_group_jQ = $('.e_follow_group', _elm);
    var mine_jQ = $('.e_mine', _elm);
    var myActive_jQ = $('.e_myActive', _elm);
    var atMe_jQ = $('.e_atMe', _elm);

    //微博参数，传送给微博工具类
    var blog_data = {};

    //========================================微博导航条========================================

    var initEveryone = function() {   //初始化大家        
        everyone_jQ.click(function() {
            blog_data.blog_type = 0;
            var blog_list_plugin = BlogUtil.createMicroblogList(blog_data);
            microblog_jQ.empty().append(blog_list_plugin);
        });
    };

    var initFriend = function() {

        //初始化“好友列表”
        Backend.findFriendGroups(null, function(_resp) {
            if (Backend.dataValidate(_resp)) {
                var newGroup = SocialUtil.sortFriendGroup(_resp['data']);
                $.each(newGroup, function(_i, _g) {
                    if (_g) {
                        var group_jQ = $('<a href="javascript:;" title="' + _g.groupName + '">' + _g.groupName + '</a>');
                        group_jQ.click(function() {
                            blog_data.group_id = _g.groupID;
                            blog_data.blog_type = 1;
                            var blog_list_plugin = BlogUtil.createMicroblogList(blog_data);
                            microblog_jQ.empty().append(blog_list_plugin);
                        });
                        friend_group_jQ.find('.e_subnav').append(group_jQ);
                    };
                });
            }
        });

        friend_group_jQ.find('p').click(function() {
            friend_group_jQ.find('.e_subnav').children().eq(0).click();
        });
    };

    var initFollow = function() {

        //初始化“关注列表”        
        Backend.findFollowGroups(null, function(_resp) {
            if (Backend.dataValidate(_resp)) {
                var newGroup = SocialUtil.sortFriendGroup(_resp['data']);
                $.each(newGroup, function(_i, _g) {
                    if (_g) {
                        var group_jQ = $('<a href="javascript:;" title="' + _g.groupName + '">' + _g.groupName + '</a>');
                        group_jQ.click(function() {
                            blog_data.group_id = _g.groupID;
                            blog_data.blog_type = 2;
                            var blog_list_plugin = BlogUtil.createMicroblogList(blog_data);
                            microblog_jQ.empty().append(blog_list_plugin);
                        });
                        follow_group_jQ.find('.e_subnav').append(group_jQ);
                    };
                });
            }
        });

        follow_group_jQ.find('p').click(function() {
            follow_group_jQ.find('.e_subnav').children().eq(0).click();
        });
    };

    var initMine = function() {

        myActive_jQ.click(function() {
            blog_data.blog_type = 3;
            var blog_list_plugin = BlogUtil.createMicroblogList(blog_data);
            microblog_jQ.empty().append(blog_list_plugin);
        });

        atMe_jQ.click(function() {
            blog_data.blog_type = 4;
            var blog_list_plugin = BlogUtil.createMicroblogList(blog_data);
            microblog_jQ.empty().append(blog_list_plugin);
        });

        mine_jQ.find('p').click(function() {
            myActive_jQ.click();
        });
    };

    //========================================页面初始化========================================

    var initPage = function() {

        //初始化导航条
        initEveryone();
        initFriend();
        initFollow();
        initMine();

        nav_jQs.hover(function() {
            $(this).find('p').addClass('active');
            $(this).find('.e_subnav').show();
        }, function() {
            $(this).find('p').removeClass('active');
            $(this).find('.e_subnav').hide();
        });

        SiteUtil.getServerZone(function(_areaList) {
            blog_data.area_list = _areaList;
            blog_data.user_id = MPT.Config["session"]["userid"];

            //初始化微博空间，默认为“大家”
            everyone_jQ.click();
        });
    };

    initPage();
});